<?php
namespace app\admin\controller;

class Index extends \think\Controller
{
    public function index()
    {
        if (!cookie('admin_id')) {
            $this->redirect('index/login');
        }
		$this->admin=db_find("admin","id=".decrypt(\cookie('admin_id')));
		$this->assign("admin",$this->admin);
        return view();
    }
    public function clear(){
        deldir(env('runtime_path'). 'temp/');
        $this->success("缓存清除成功");
    }
     
    public function login()
    {
        if ($this->request->isPost()) {
			$map['name']=input('name');
			$map['pass']=sha1(input('pass'));
			$admin=db_find("admin",$map);
			if(empty($admin)){
				$this->error("用户名或密码错误");
			}
            cookie('admin_id', encrypt($admin['id']));
            $this->success('登录成功', 'index');
        }
        return view();
    }
	function logout(){
		\cookie('admin_id',null);
		$this->success('', 'login');
	}
	
    public function upload()
    {
        $file = request()->file('imgFile');
        if ($file) {
            $info = $file->move(env('root_path').'/static/upload');
            $data=[];
            if ($info) {
                $data['error']=0;
                $data['url']="/static/upload/".\str_replace('\\',"/",$info->getSaveName()) ;
            }else{
				$data['error']=1;
				$data['message'] = $file->getError();
			}
        }else{
			$data['code']=1;
			$data['message'] = "上传失败";
		}
       
        echo \json_encode($data);
        exit;
    }
}
